﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KMFrame.Permission.NBL;
using KMFrame.Permission.NBL.DBEntities;
using KMFrame.Permission.Common;
using KMFrame.Permission.Util;
using NBearLite;
using KMFrame.Util;

namespace KMFrame.Permission.BLL
{
    public class Users : PermissionLogic
    {
        public IEnumerable<ShowUserList> ShowUserList(WhereClip where)
        {
            Roles roles = new Roles();
            IEnumerable<KM_Users> ulist = NBLData.Select(DBColumns.KM_Users).Where(where).ToList<KM_Users>();
            string[] gq = (from l in ulist select l.UserGroup.ToString()).ToArray();
            IEnumerable<KM_Groups> glist = NBLData.Select(DBColumns.KM_Groups).Where(DBColumns.KM_Groups.GroupID.In(gq)).ToList<KM_Groups>();
            IList<UserStatus> slist = UserHandle.StateList();
            var query = from l in ulist
                        join m in glist
                        on l.UserGroup equals m.GroupID
                        join n in slist
                        on l.Status equals n.ID
                        select new ShowUserList
                        {
                            UserID = l.UserID,
                            UserName = l.UserName,
                            RoleName = roles.GetUserRoles(l.UserID),
                            GroupName = m.GroupName,
                            Email = l.Email,
                            Status = n.Status,
                            CreateTime = l.CreateTime,
                            LastLoginTime = l.LastLoginTime
                        };
            return query;
        }

        public UserInfo GetUserInfo(int UserID)
        {
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                DBColumns.KM_Users.UserID, 
                DBColumns.KM_Users.UserName,
                DBColumns.KM_Users.UserGroup,
                DBColumns.KM_Users.Email,
                DBColumns.KM_Users.Status,
                DBColumns.KM_Users.CreateTime,
                DBColumns.KM_Users.LastLoginTime
            };
            return NBLData
                .Select(DBColumns.KM_Users, selectColumns)
                .Where(DBColumns.KM_Users.UserID==UserID)
                .ToSingleObject<UserInfo>();
        }

        public bool UpdateUserInfo(UserInfo User)
        {
            bool result = false;
            UpdateSqlSection update = NBLData.Update(DBColumns.KM_Users)
                .AddColumn(DBColumns.KM_Users.Email, User.Email)
                .AddColumn(DBColumns.KM_Users.UserName, User.UserName)
                .AddColumn(DBColumns.KM_Users.UserGroup, User.UserGroup)
                .AddColumn(DBColumns.KM_Users.Status, User.Email);
            int ri = update
                .Where(DBColumns.KM_Users.UserID == User.UserID)
                .Execute();
            if (ri == 1)
                result = true;
            return result;
        }

        public bool CreateUser(UserInfo User)
        {
            bool result = false;
            InsertSqlSection insert = NBLData.Insert(DBColumns.KM_Users)
                .AddColumn(DBColumns.KM_Users.UserName, User.UserName)
                .AddColumn(DBColumns.KM_Users.Email, User.Email)
                .AddColumn(DBColumns.KM_Users.UserGroup, User.UserGroup)
                .AddColumn(DBColumns.KM_Users.Status, User.Status)
                .AddColumn(DBColumns.KM_Users.Password, SecurityHelper.MD5("111111",32))
                .AddColumn(DBColumns.KM_Users.CreateTime, DateTime.Now.ToString());
            int ri = insert.Execute();
            if (ri == 1)
                result = true;
            return result;
        }
    }

    public class UserInfo
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public int UserGroup { get; set; }
        public string Email {get;set;}
        public int Status { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime? LastLoginTime { get; set; }
    }

    /// <summary>
    /// 列举用户清单
    /// </summary>
    public class ShowUserList
    {
        public int UserID { get; set; }
        public string UserName { get; set; }
        public string GroupName { get; set; }
        public string RoleName { get; set; }
        public string Email { get; set; }
        public string Status { get; set; }
        public DateTime CreateTime { get; set; }
        public DateTime? LastLoginTime { get; set; }
    }
}
